/*
 * Copyright (C) 2012-2013 Michael L.R. Marques
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact: michaellrmarques@gmail.com
 */

package com.jm.commons.components.panel;

import com.jm.commons.fio.FileSystem;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;


/**
 * Allows an IDE to choose an image for a JavaBean's image property
 * 
 * @author Michael L.R. Marques
 */
public class ImageChooser extends javax.swing.JPanel implements java.beans.Customizer {
    
    /**
     * 
     */
    public File bean;

    /**
     * Creates new customizer BackgroundImageChooser
     */
    public ImageChooser() {
        initComponents();
    }
    
    /**
     * 
     * @param bean 
     */
    @Override public void setObject(Object bean) {
        // 
        if (bean instanceof String) {
            if (bean != null &&
                    !bean.toString().isEmpty() &&
                        !new File(bean.toString()).exists()) {
                bean = new File(bean.toString());
            }
        }
        // 
        if (bean instanceof File &&
                bean != null &&
                    ((File) bean).exists()) {
            this.bean = (File) bean;
            this.txtFile.setText(this.bean.getAbsolutePath());
            this.pnlImage.setImage(this.bean);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the FormEditor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        txtFile = new com.jm.commons.components.textfield.HintJTextField();
        btnImageBrowse = new javax.swing.JButton();
        pnlImage = new com.jm.commons.components.panel.ImagePanel();

        txtFile.setEditable(false);
        txtFile.setHintText("Select Image File...");

        btnImageBrowse.setText("...");
        btnImageBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectImage(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtFile, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnImageBrowse, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnImageBrowse))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlImage.setToolTipText("");

        javax.swing.GroupLayout pnlImageLayout = new javax.swing.GroupLayout(pnlImage);
        pnlImage.setLayout(pnlImageLayout);
        pnlImageLayout.setHorizontalGroup(
            pnlImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 521, Short.MAX_VALUE)
        );
        pnlImageLayout.setVerticalGroup(
            pnlImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 322, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void SelectImage(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectImage
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Select Image...");
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setCurrentDirectory(new File(this.txtFile.getText()));
        fileChooser.setFileFilter(ImageFileFilter.getInstance());
        if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(null)) {
            this.pnlImage.setImage(fileChooser.getSelectedFile());
            try {
                this.txtFile.setText(fileChooser.getSelectedFile().getCanonicalPath());
            } catch (IOException ioe) {
                this.txtFile.setText(fileChooser.getSelectedFile().getAbsolutePath());
                System.out.println(ioe.getMessage());
            }
            this.pnlImage.setImage(fileChooser.getSelectedFile());
        }
    }//GEN-LAST:event_SelectImage

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImageBrowse;
    private javax.swing.JPanel jPanel2;
    private com.jm.commons.components.panel.ImagePanel pnlImage;
    private com.jm.commons.components.textfield.HintJTextField txtFile;
    // End of variables declaration//GEN-END:variables
}
/**
 * 
 * @author Michael L.R. Marques
 */
class ImageFileFilter extends FileFilter {

    /**
     * 
     */
    private ImageFileFilter() {
        super();
    }

    /**
     * Creates an instance of itself
     * @return FileFilter
     */
    public static FileFilter getInstance() {
        return new ImageFileFilter();
    }

    /**
     * 
     * @param f
     * @return boolean
     */
    @Override public boolean accept(File f) {
        return FileSystem.isValidExtension(f, new String[] {"gif", "jpg", "png", "bmp", "ico"}) ||
                    f.isDirectory();
    }

    /**
     * 
     * @return String
     */
    @Override public String getDescription() {
        return "Images (*.gif, *.jpg, *.png, *.bmp, *.ico)";
    }

}
